package org.thss.petri.gef.figures;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.handles.HandleBounds;

public class NodeFigure extends Figure implements HandleBounds {
	public static int NAME_LABEL_HEIGHT = 20;
	
	protected Label nameLabel;
	protected Figure shape;
	
	public NodeFigure() {
		nameLabel = new Label();
		add(nameLabel);
		setLayoutManager(new XYLayout());
	}
	
	public IFigure getNodeShape() {
		return shape;
	}
	
	@Override
	protected void paintFigure(Graphics graphics) {
		Rectangle bound = getBounds();
		setConstraint(shape, new Rectangle(0, 0,
				bound.width, bound.height - NAME_LABEL_HEIGHT));
		setConstraint(nameLabel, new Rectangle(0, bound.height - NAME_LABEL_HEIGHT, bound.width, NAME_LABEL_HEIGHT));
		shape.invalidate();
		nameLabel.invalidate();
	}
	
	@Override
	public Rectangle getHandleBounds() {
		Rectangle bound = getBounds().getCopy();
		bound.height -= NAME_LABEL_HEIGHT;
		return bound;
	}
	
	public void setName(String name) {
		nameLabel.setText(name);
	}
}
